package zhongbo.javaweb.instagram.service.runner;

import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import zhongbo.javaweb.instagram.model.Task;
import zhongbo.javaweb.instagram.service.model.TaskService;
import zhongbo.javaweb.instagram.util.thread.ProgressProcessor;
import zhongbo.javaweb.instagram.util.thread.ProgressTask;

import java.util.List;
import java.util.Queue;

/**
 * 说明：
 *
 * @author zhongboWork
 * @version 1.0
 * @since 2021-12-17 11:32
 */
@Slf4j
public class InstagramTask extends ProgressTask<Document> {

    private Task task;

    private TaskService taskService;

    public InstagramTask(String taskName, int threadSize, Queue<Document> targetList, ProgressProcessor<Document> processor, Task task, TaskService taskService) {
        super(taskName, threadSize, targetList, processor);
        this.task = task;
        this.taskService = taskService;
        log.info("已创建 InstagramTask, taskName={} threadSize={} targetList.size()={} processor={}", taskName, threadSize, targetList, processor);
    }

    @Override
    protected void updateTask() {
        int current = this.getFinishedList().size();
        int total = this.getTargetList().size();
        int percent = current / total;
        boolean running = this.isRunning();
        boolean paused = this.isPaused();
        int runningCount = this.getRunningCount().intValue();
        task.setCurrent(current);
        task.setTotal(total);
        task.setPaused(paused);
        task.setRunning(running);
        task.setRunningThread(runningCount);
        task.setPercentSuccess(percent);
        taskService.addOrUpdate(task);
    }
}
